<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/4/26
 * Time: 14:45
 */

namespace App\Rep;


use App\Models\OrderChild;
use Illuminate\Support\Facades\DB;

class SalaryRep
{
    //员工薪资列表
    public function getSalaryListRep($params){

        $list = OrderChild::Search($params)
            ->whereNotNull('employee_user_id')
            ->with(['employee'=>function($q){
                $q->select('employee_name','base_pay','user_id','id');
            }])
            ->select(
                'employee_user_id','id',
                DB::raw('sum(price) as total_sales'),
                DB::raw('count(*) as order_count')
            )
            ->groupBy('employee_user_id')
            ->paginate($params['per_page']??30);


        return $list;

    }

    /**
     * 获取员工薪资详情
     * @param $params
     */
    public function getSalaryInfoRep($params){
        $list = OrderChild::Search($params)
            ->with(['order'=>function($q){
                $q->with(['color'=>function($q){
                    return $q->select('id','color');
                },'product'=>function($q){
                    return $q->select('id','product_name');
                },'customer'=>function($q){
                    return $q->select('id','customer_name','customer_address');
                },'modelNumber'=>function($q){
                    return $q->select('id','model_number');
                },'specification'=>function($q){
                    return $q->select('id','specification');
                }])
                    ->select('id','order_number','color_id','product_id','customer_id','model_number_id','specification_id');
            },
                'employee'=>function($q){
                $q->select('user_id','employee_name','base_pay','id');
                }
            ])
            ->select('id','order_id','price','employee_user_id','submit_time')
            ->paginate($params['per_page']??10);
        return $list;
    }

    /**
     * 修改员工工资
     * @param $params
     */
    public function changeSalaryRep($params){
        $order_child_id = $params['order_child_id'];
        $model = OrderChild::find($order_child_id);
        $model->price = $params['price'];
        $model->save();
        return $model;
    }



}
